//<!-- License -->
package eu.datafusion.log4soa.model.referencedata;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

/**
 * NamedEntity is base class for entities with unique name
 * @author Ivo.Mitov@datafusion.eu
 *
 */
@MappedSuperclass
public abstract class NamedEntity implements Serializable{
	
	/**
	 * generated
	 */
	private static final long serialVersionUID = -8695403876150454385L;

	/**
	 * entity id -  primary key
	 */
	private int id;
	
	/**
	 * Entity version
	 */
	@Version
	@Column(name="VERSION")
	private int version;
	
	/**
	 * name is unique, required and immutable
	 */
	@Column(name="NAME", nullable = false, unique = true,length = 255)
	private String name;
	
	/**
	 * optional description
	 */
	@Column(name="DESCRIPTION",length = 1024, nullable = true)
	private String description;
	
	/**
	 * default constructor required by JPA
	 */
	protected NamedEntity() {
	
	}

	/**
	 * @param name required property <tt>name</tt>, must be unique per concrete class instances
	 */
	protected NamedEntity(String name) {
		this(name,null);
	}
	
	/**
	 * @param name required property <tt>name</tt>, must be unique per concrete class instances
	 * @param description optional property <tt>description</tt>
	 */
	protected NamedEntity(String name, String description) {
		super();
		this.setName(name);
		this.setDescription(description);
	}
	
	/** 
	 * Getter of the primary key <tt>id</tt> 
	 * @return  Returns the id.
	 */
	public int getId() {
		return id;
	}

	/**
	 * Getter of the unique required immutable property <tt>name</tt> 
	 * @return  Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Setter of the  required property <tt>name</tt> is used only in constructor
	 * @param name required property <tt>name</tt> , must be unique per concrete class instances
	 */
	private void setName(String name) {
		if(name==null || name.length()==0){
			throw new IllegalArgumentException("name can't be null"); 
		}
		this.name = name;
	}

	/**
	 * Getter of the optional property <tt>description</tt> 
	 * @return  Returns the description.
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Setter of the optional property <tt>description</tt>
	 * @param description 
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		NamedEntity other = (NamedEntity) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public String toString(){
		return this.getClass().getName() +":" +"id=" + id + ", version=" + version +
					", name="+ name + ", description=" + description;
	}

	
	

}
